/*---------------------------------------------------------------------------*/
#include <QtGui>
#include "MenuBox.h"
#include <string>
#include <iostream>
#include "Config.h"
#include "Utils.h"
#include "MenuButton.h"
/*---------------------------------------------------------------------------*/
using namespace std;
/*---------------------------------------------------------------------------*/
MenuBox::MenuBox(BrowserBox *_flw) : QHBoxLayout()
{
	
	// widgets
	flw = _flw;
	
	buttonHome = new MenuButton( "", "home.png", 26, 26, "Go Home" );
	buttonTerminal = new MenuButton( "", "terminal.png", 26, 26, "Go Home" );
	buttonRefresh = new MenuButton("", "refresh.png", 26, 26, "Go Home" );
	buttonUp = new MenuButton( "", "go-up.png", 26, 26, "Go Home" );
	addressBar = new QLineEdit("");
	buttonOptions = new MenuButton("   ", "settings.png", 46, 26, "Settings" );
	
	this->setSpacing(1);
	this->setContentsMargins(1,1,1,1);
	
	
	
	
	this->addWidget( buttonHome );
	this->addWidget( buttonTerminal );
	this->addWidget( buttonRefresh );
	this->addWidget( buttonUp );
	this->addWidget( addressBar );
	this->addWidget( buttonOptions );
	

	// actions
	QAction				*actionHidden;				
	QAction				*actionSwapFiles;
	QAction				*actionConfig;
	
	actionHidden = new QAction("Show hidden files", this);
	actionHidden->setCheckable(true);
	actionSwapFiles = new QAction("Show swap files", this);
	actionSwapFiles->setCheckable(true);
	actionConfig = new QAction("View/edit config", this);
		
		
		
	// menu options
	menuOptions.setTitle( "Options" );
	menuOptions.addAction( actionHidden );
	menuOptions.addAction( actionSwapFiles );
	menuOptions.addSeparator();
	menuOptions.addAction( actionConfig );
	
	buttonOptions->setMenu( &menuOptions );	
	
	
	// connections

	connect( actionHidden, SIGNAL( toggled(bool) ), this, SLOT( toggleHidden(bool) ) ); 
	connect( actionSwapFiles, SIGNAL( toggled(bool) ), this, SLOT( toggleSwapFiles(bool) ) ); 
	connect( actionConfig, SIGNAL( triggered(bool) ), this, SLOT( editConfig(bool) ) ); 
	
	connect( buttonHome, SIGNAL( clicked() ), this, SLOT( buttonHomeClicked() ) );
	connect( buttonTerminal, SIGNAL( clicked() ), this, SLOT( buttonTerminalClicked() ) );
	connect( buttonRefresh, SIGNAL( clicked() ), this, SLOT( buttonRefreshClicked() ) );
	connect( buttonUp, SIGNAL( clicked() ), this, SLOT( buttonUpClicked() ) );
	
	connect( addressBar, SIGNAL( returnPressed() ), this, SLOT( returnPressed() ) );
	
}
/*---------------------------------------------------------------------------*/
MenuBox::~MenuBox()
{
	
}
/*---------------------------------------------------------------------------*/
void MenuBox::setAddressBar(const char *path)
{
	addressBar->setText(path);
}
/*---------------------------------------------------------------------------*/
void MenuBox::toggleHidden(bool checked)
{
	flw->showHidden(checked);
	flw->fillFromCurrentNode();
}
/*---------------------------------------------------------------------------*/
void MenuBox::toggleSwapFiles(bool checked)
{
	flw->showSwapFiles(checked);
	flw->fillFromCurrentNode();
}
/*---------------------------------------------------------------------------*/
void MenuBox::buttonHomeClicked()
{
	flw->goHome();	
}
/*---------------------------------------------------------------------------*/
void MenuBox::buttonTerminalClicked()
{
	string str = Config::getValue("$terminal");
	str += " ";
	str += Utils::escapeChars( flw->getCurrentPath() );
	str += " &";

	system( str.c_str() );
}
/*---------------------------------------------------------------------------*/
void MenuBox::buttonRefreshClicked()
{
	flw->refresh();
}
/*---------------------------------------------------------------------------*/
void MenuBox::buttonUpClicked()
{
	flw->goParent();
}
/*---------------------------------------------------------------------------*/
void MenuBox::returnPressed()
{
	if( Utils::isDirectory( addressBar->text().toStdString() ) )	
	{
		flw->goTo( (char*)addressBar->text().toStdString().c_str() );
	}
	else
	{
		addressBar->setText( QString( flw->getCurrentPath().c_str() ) );
	}
		
}
/*---------------------------------------------------------------------------*/
void MenuBox::editConfig(bool checked)
{
	string cmd = Config::getValue("$editor") + " " + Utils::escapeChars(getenv("HOME")) + "/.gfm";
	DEBUGMSG( "MenuBox::editConfig()" );
	system(cmd.c_str());
	
	// reload config
	string stemp = getenv("HOME");
	stemp += "/.gfm";
	Config::loadConfig(stemp.c_str());
}
/*---------------------------------------------------------------------------*/




